package org.nnxy.foodboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nnxy.foodboot.entity.PopularRecipe;
import org.nnxy.foodboot.entity.Recipe;
import org.nnxy.foodboot.mapper.PopularRecipeMapper;
import org.nnxy.foodboot.service.PopularRecipeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 热门食谱服务实现类
 */
@Service
public class PopularRecipeServiceImpl extends ServiceImpl<PopularRecipeMapper, PopularRecipe> implements PopularRecipeService {

    @Autowired
    private PopularRecipeMapper popularRecipeMapper;

    /**
     * 获取热门食谱列表
     *
     * @param limit 限制条数
     * @return 热门食谱列表
     */
    @Override
    public List<Recipe> getPopularRecipes(Integer limit) {
        return popularRecipeMapper.getPopularRecipesWithDetails(limit);
    }

    /**
     * 更新食谱热门指标
     *
     * @param recipeId 食谱ID
     * @param viewIncrement 浏览增量
     * @param likeIncrement 点赞增量
     * @param collectIncrement 收藏增量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRecipePopularityMetrics(Long recipeId, Integer viewIncrement, Integer likeIncrement, Integer collectIncrement) {
        // 查询现有记录
        PopularRecipe popularRecipe = this.getOne(
                new LambdaQueryWrapper<PopularRecipe>()
                        .eq(PopularRecipe::getRecipeId, recipeId)
        );
        
        // 如果不存在，创建新记录
        if (popularRecipe == null) {
            popularRecipe = new PopularRecipe();
            popularRecipe.setRecipeId(recipeId);
            popularRecipe.setViewCount(0);
            popularRecipe.setLikeCount(0);
            popularRecipe.setCollectCount(0);
            popularRecipe.setScore(BigDecimal.ZERO);
            popularRecipe.setRank(9999); // 默认一个很大的排名
        }
        
        // 更新计数
        if (viewIncrement != null && viewIncrement > 0) {
            popularRecipe.setViewCount(popularRecipe.getViewCount() + viewIncrement);
        }
        if (likeIncrement != null && likeIncrement > 0) {
            popularRecipe.setLikeCount(popularRecipe.getLikeCount() + likeIncrement);
        }
        if (collectIncrement != null && collectIncrement > 0) {
            popularRecipe.setCollectCount(popularRecipe.getCollectCount() + collectIncrement);
        }
        
        // 计算热门得分 - 根据浏览、点赞、收藏的权重计算
        // 这里的权重可以根据业务需求调整
        BigDecimal score = new BigDecimal(
                popularRecipe.getViewCount() * 1 +
                popularRecipe.getLikeCount() * 5 +
                popularRecipe.getCollectCount() * 10
        );
        popularRecipe.setScore(score);
        
        // 保存或更新
        this.saveOrUpdate(popularRecipe);
    }

    /**
     * 刷新所有热门食谱排名
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void refreshPopularRecipeRanks() {
        // 按分数查询所有热门食谱
        List<PopularRecipe> popularRecipes = this.list(
                new LambdaQueryWrapper<PopularRecipe>()
                        .orderByDesc(PopularRecipe::getScore)
        );
        
        // 更新排名
        AtomicInteger rank = new AtomicInteger(1);
        for (PopularRecipe recipe : popularRecipes) {
            recipe.setRank(rank.getAndIncrement());
        }
        
        // 批量更新
        this.updateBatchById(popularRecipes);
    }
} 